/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 4/21/13
 * Time: 5:39 PM
 * To change this template use File | Settings | File Templates.
 */
package societyGames
{
import flexunit.framework.Assert;

import mx.collections.ArrayList;

import societyGames.xmlFragment.XMLFragmentCollection;
import societyGames.xmlFragmentTest.PeopleContainer;
import societyGames.xmlFragmentTest.Person;
import societyGames.xmlFragmentTest.Pet;

public class XMLFragmentCollectionTest
{
  public function XMLFragmentCollectionTest()
  {
  }

  [Test(expects="RangeError")]
  public function testRemoveItemAt():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);

    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.removeItemAt(0);
    Assert.assertEquals(xml.person.length(), 1);
    Assert.assertEquals(xmlFragmentCollection.length, 1);
    Assert.assertEquals(xml.person[0].@name, "Sally");
    xmlFragmentCollection.removeItemAt(0);
    Assert.assertEquals(xml.person.length(), 0);
    Assert.assertEquals(xmlFragmentCollection.length, 0);
    xmlFragmentCollection.removeItemAt(0);
  }

  [Test(expects="ArgumentError")]
  public function testAddItem():void
  {
    var xml:XML = <people/>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var person:Person = Person(peopleContainer.createChildXMLFragment(Person));
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.addItem(person);
    Assert.assertEquals(xml.person.length(), 1);
    Assert.assertEquals(xml.person[0].@name, "Default");
    person = Person(peopleContainer.createChildXMLFragment(Person));
    xmlFragmentCollection.addItem(person);
    Assert.assertEquals(xml.person.length(), 2);
    Assert.assertEquals(xml.person[1].@name, "Default");
    var pet:Pet = Pet(peopleContainer.createChildXMLFragment(Pet));
    xmlFragmentCollection.addItem(pet);
  }

  [Test(expects="ArgumentError")]
  public function testAddAllAt():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var persons:ArrayList = new ArrayList([Person(peopleContainer.createChildXMLFragment(Person)), Person(peopleContainer.createChildXMLFragment(Person))]);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.addAllAt(persons, 1);
    Assert.assertEquals(xml.person.length(), 4);
    Assert.assertEquals(xml.person[1].@name, "Default");
    Assert.assertEquals(xml.person[2].@name, "Default");
    persons.removeAll();
    var pet:Pet = Pet(peopleContainer.createChildXMLFragment(Pet));
    persons.addItem(pet);
    xmlFragmentCollection.addAllAt(persons, 1);
  }

  [Test(expects="RangeError")]
  public function testSetItemAt():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.setItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 0);
    Assert.assertEquals(xml.person.length(), 2);
    Assert.assertEquals(xmlFragmentCollection.length, 2);
    Assert.assertEquals(xml.person[0].@name, "Default");

    xmlFragmentCollection.setItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 1);
    Assert.assertEquals(xml.person.length(), 2);
    Assert.assertEquals(xmlFragmentCollection.length, 2);
    Assert.assertEquals(xml.person[1].@name, "Default");

    xmlFragmentCollection.setItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 2);
  }

  [Test]
  public function testAddItemAt():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.addItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 0);
    Assert.assertEquals(xml.person.length(), 3);
    Assert.assertEquals(xmlFragmentCollection.length, 3);
    Assert.assertEquals(xml.person[0].@name, "Default");

    xmlFragmentCollection.addItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 2);
    Assert.assertEquals(xml.person.length(), 4);
    Assert.assertEquals(xmlFragmentCollection.length, 4);
    Assert.assertEquals(xml.person[2].@name, "Default");

    xmlFragmentCollection.addItemAt(Person(peopleContainer.createChildXMLFragment(Person)), 4);
    Assert.assertEquals(xml.person.length(), 5);
    Assert.assertEquals(xmlFragmentCollection.length, 5);
    Assert.assertEquals(xml.person[4].@name, "Default");
  }

  [Test(expects="ArgumentError")]
  public function testAddAll():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    var persons:ArrayList = new ArrayList([Person(peopleContainer.createChildXMLFragment(Person)), Person(peopleContainer.createChildXMLFragment(Person))]);
    xmlFragmentCollection.addAll(persons);
    Assert.assertEquals(xml.person.length(), 4);
    Assert.assertEquals(xml.person[2].@name, "Default");
    Assert.assertEquals(xml.person[3].@name, "Default");
    persons.removeAll();
    var pet:Pet = Pet(peopleContainer.createChildXMLFragment(Pet));
    persons.addItem(pet);
    xmlFragmentCollection.addAll(persons);
  }

  [Test]
  public function testRemoveAll():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    xmlFragmentCollection.removeAll();
    Assert.assertEquals(xml.person.length(), 0);
    Assert.assertEquals(xmlFragmentCollection.length, 0);
    xmlFragmentCollection.removeAll();
    Assert.assertEquals(xml.person.length(), 0);
    Assert.assertEquals(xmlFragmentCollection.length, 0);
    xmlFragmentCollection.addItem(Person(peopleContainer.createChildXMLFragment(Person)));
    xmlFragmentCollection.removeAll();
    Assert.assertEquals(xml.person.length(), 0);
    Assert.assertEquals(xmlFragmentCollection.length, 0);
  }

  [Test]
  public function testFragment():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
        <spouse name="Mandy">
          <pet>Fish</pet>
        </spouse>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    var spouse:Person = Person(xmlFragmentCollection[0]).spouse;
    Assert.assertEquals(spouse.name, "Mandy");
  }

  [Test(expects="ArgumentError")]
  public function testBadMultipleFragment():void
  {
    var xml:XML = <people>
      <person name="Paul">
        <pet>Rover</pet>
        <pet>Max</pet>
        <spouse name="Mandy">
          <pet>Fish</pet>
        </spouse>
        <spouse name="Sandy">
          <pet>Spot</pet>
        </spouse>
      </person>
      <person name="Sally">
        <pet>Spot</pet>
        <pet>Thomas</pet>
      </person>
    </people>;
    var peopleContainer:PeopleContainer = new PeopleContainer();
    peopleContainer.initialize(peopleContainer, xml);
    var xmlFragmentCollection:XMLFragmentCollection = peopleContainer.persons;
    var spouse:Person = Person(xmlFragmentCollection[0]).spouse;
    Assert.assertEquals(spouse.name, "Mandy");
  }
}
}
